package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.CompanySelectAgreeRequest;
import com.jmp.api.admin.dto.CompanySelectRequest;
import com.jmp.api.admin.dto.CompanySelectResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.facade.CompanyFacadeService;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.mapper.CompanyMapperExtPublic;
import one.stand.model.Company;
import one.stand.model.CompanySelect;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.util.SqlUtil;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("adminApi/company/select")
@Api(description = "优选审核")
@Slf4j
public class CompanySelectController {
    public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy.MM.dd");
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private CompanyFacadeService companyFacadeService;
    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;
    @Autowired
    private CompanyMapperExtPublic companyMapperExtPublic;

    @ApiOperation("列表")
    @GetMapping("")
    @SecurityAuthCheck
    public ResultModel<CompanySelectResponse> list(CompanySelectRequest request) {

        String companyLikeName = SqlUtil.like(request.getCompanyName());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanySelect> selects = companyApiAdminMapper.selectSelect(request.getType(), companyLikeName, request.getSelectCompanyId());

        PageResponse2<CompanySelectResponse.CompanySelect> list = ConvertUtils.page2(selects, select -> {
            Company selectCompany = companyBaseService.getById(select.getSelectCompanyId());
            Company company = companyBaseService.getById(select.getCompanyId());
            return CompanySelectResponse.CompanySelect.builder()
                    .id(select.getId())
                    .selectCompanyName(selectCompany.getCompanyName())
                    .companyName(company.getCompanyName())
                    .status(select.getStatus())
                    .applyTime(DATE_FORMAT.format(select.getCreateTime()))
                    .build();
        });

        List<CompanySelectResponse.SelectCompany> selectCompanies = null;
        if (request.getPageNum() == 1) {
            List<Company> companies = companyMapperExtPublic.selectCompanySelectList();
            selectCompanies = ConvertUtils.list(companies, company -> CompanySelectResponse.SelectCompany.builder()
                    .companyId(company.getCompanyId())
                    .companyName(company.getCompanyName())
                    .build());
        }

        CompanySelectResponse response = CompanySelectResponse.builder()
                .list(list)
                .selectCompanyList(selectCompanies)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("通过")
    @PostMapping("agree")
    @SecurityAuthCheck
    public ResultModel agree(CompanySelectAgreeRequest request) {
        companyFacadeService.selectAgree(request.getId());

        return ResultModel.success();
    }

    @ApiOperation("拒绝")
    @PostMapping("refuse")
    @SecurityAuthCheck
    public ResultModel refuse(CompanySelectAgreeRequest request) {
        companyFacadeService.selectRefuse(request.getId());

        return ResultModel.success();
    }
}
